//
//  ScenicSecondViewController.m
//  小胖兔旅游指南
//
//  Created by KANGEL on 15/4/13.
//  Copyright (c) 2015年 小胖兔. All rights reserved.
//

#import "ScenicSecondViewController.h"
#import "EncapsulationMethod.h"
#import "UIImageView+WebCache.h"

@interface ScenicSecondViewController ()<UIScrollViewDelegate>
{
    UIScrollView *_scrollView;
    UIPageControl *_page;
    CGRect rect;
}

@property (nonatomic, retain) NSMutableArray *secondArray;

@property (nonatomic, retain) NSMutableDictionary *secondDic;

@property (nonatomic, retain) UIScrollView *scrollSecond;//图片翻滚

@property (nonatomic, retain) UILabel *pageLabel;

@end

@implementation ScenicSecondViewController

- (void)dealloc
{
    [_secondArray release];
    [_secondDic release];
    [_scrollSecond release];
    [super dealloc];
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.secondArray = [NSMutableArray array];
        
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self createScenicSecond];
//    NSLog(@"%@", self.myId);
    self.view.backgroundColor = [UIColor whiteColor];
    
}


//http://tubu.ibuzhai.com/rest/v1/trail/735?app_version=1.9.0&device_type=1

#pragma mark - 
#pragma mark 第二界面搭建
- (void)createScenicSecond
{
    //整个页面
    _scrollView = [[[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)] autorelease];
    [self.view addSubview:_scrollView];
    
//详情字符串拼接
//        NSString * str = @"http://tubu.ibuzhai.com/rest/v1/trail/735?app_version=1.9.0&device_type=1";
//        NSString * urlStr = [str stringByReplacingOccurrencesOfString:@"/735" withString:self.myId];//替换字符串中的某一小段
    
    NSString *url1 = @"http://tubu.ibuzhai.com/rest/v1/trail/";//上面的方法不适合
    NSString *urlQ = [NSString stringWithFormat:@"%@%@", url1, self.myId];
    
    //网络请求 获取数据
    [EncapsulationMethod connectWithUrl:urlQ parmaters:nil finishBlock:^(NSData *data) {
        self.secondDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSLog(@"secondDic ========= %@", _secondDic);
        self.secondArray = [_secondDic objectForKey:@"pictures"];
        
        //展示图
        [self showALL];
    }];
}

#pragma mark -
#pragma mark 界面展示内容

- (void)showALL
{
    [self showScrollView];
    [self showText];
}

#pragma mark -
#pragma mark 滚动景点视图
- (void)showScrollView
{
    
    self.scrollSecond = [[[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, _scrollView.frame.size.width, 200)] autorelease];
    _scrollSecond.contentSize = CGSizeMake(_scrollView.frame.size.width * 15, 200);
    _scrollSecond.bounces = NO;
    _scrollSecond.pagingEnabled = YES;//整页翻动
    _scrollSecond.delegate = self;
    
    //展示图中每个图
    for (int i = 1; i <= 15; i++) {
        UIScrollView *smallScroll = [[[UIScrollView alloc] initWithFrame:CGRectMake((i - 1) * _scrollSecond.frame.size.width, 0, _scrollSecond.frame.size.width, _scrollSecond.frame.size.height)] autorelease];
        smallScroll.delegate = self;
        [_scrollSecond addSubview:smallScroll];
        NSDictionary *dict = [self.secondArray objectAtIndex:(i - 1)];
        
        UIImageView *aImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, _scrollSecond.frame.size.width, 200)];
        [aImage sd_setImageWithURL:[NSURL URLWithString:[dict objectForKey:@"picture"]]];
        [smallScroll addSubview:aImage];
        
    }
    [_scrollView addSubview:_scrollSecond];
    
    //page设置
    _page = [[UIPageControl alloc] initWithFrame:CGRectMake(0, _scrollSecond.frame.size.height - 20, _scrollSecond.frame.size.width, 20)];
    [_page setBackgroundColor:[UIColor clearColor]];
    [_page setNumberOfPages:15];
    [_scrollView addSubview:_page];
    [_page addTarget:self action:@selector(pageAction:) forControlEvents:UIControlEventTouchUpInside];
    [_page release];
    
    //pageLabel
    self.pageLabel = [[UILabel alloc] initWithFrame:CGRectMake(_scrollSecond.frame.size.width - 60, _scrollSecond.frame.size.height, 60, 25)];
    [_pageLabel setTextColor:[UIColor darkGrayColor]];
    _pageLabel.text = [NSString stringWithFormat:@"%ld / 15", _page.currentPage + 1];//防止刚进入第二界面时，开始不显示 标签1/15。
//    pageLabel.textAlignment = NSTextAlignmentRight;
    [_scrollView addSubview:_pageLabel];
    
    
}
//page的点击时间:让page与图片序号匹配上
- (void)pageAction:(UIPageControl *)sender
{
    NSInteger num = sender.currentPage;
    _scrollSecond.contentOffset = CGPointMake(num * _scrollSecond.frame.size.width, 0);
    _pageLabel.text = [NSString stringWithFormat:@"%ld / 15", num + 1];
}

//让图片与page相匹配
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if (scrollView == _scrollSecond) {
        CGPoint point = scrollView.contentOffset;
        NSInteger num = point.x / _scrollSecond.frame.size.width;
        _page.currentPage = num;
        _pageLabel.text = [NSString stringWithFormat:@"%ld / 15", num + 1];
    }
}

#pragma mark -
#pragma mark 景点介绍详情
- (void)showText
{
    //景点名
    UILabel *labelTitle = [[UILabel alloc] initWithFrame:CGRectMake(10, _scrollSecond.frame.size.height + 30, _scrollView.frame.size.width - 20, 40)];
    [_scrollView addSubview:labelTitle];
    labelTitle.text = [_secondDic objectForKey:@"name"];
    labelTitle.font = [UIFont boldSystemFontOfSize:24];//粗体
    [labelTitle release];
   
    
    //景点地（小图+地址）
    UIImage *aImage = [UIImage imageNamed:@"dingwei.png"];
    UIImageView *backImage = [[UIImageView alloc] initWithFrame:CGRectMake(10, labelTitle.frame.size.height + labelTitle.frame.origin.y, 20, 20)];
    [backImage setImage:aImage];
    [_scrollView addSubview:backImage];
    [backImage release];
    
    UILabel *labelAdd = [[UILabel alloc] initWithFrame:CGRectMake(30, labelTitle.frame.size.height + labelTitle.frame.origin.y, _scrollView.frame.size.width - 60, 20)];
    labelAdd.text = [_secondDic objectForKey:@"destination"];
    labelAdd.font = [UIFont systemFontOfSize:13];
    [_scrollView addSubview:labelAdd];
    
    
    //景点介绍
    NSString *urlStr = [_secondDic objectForKey:@"detail"];
    
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    [dic setObject:[UIFont systemFontOfSize:16] forKey:NSFontAttributeName];
    //_scrollView.frame.size.width代表宽  10000代表文本大小            ---->自适应
    CGSize size = [urlStr boundingRectWithSize:CGSizeMake(_scrollView.frame.size.width - 20, 10000) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil].size;
    
    
    UILabel *knowLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, labelAdd.frame.origin.y + labelAdd.frame.size.height + 20, _scrollView.frame.size.width - 20, size.height)];
    knowLabel.text = urlStr;
    knowLabel.numberOfLines = 0;
    knowLabel.font = [UIFont systemFontOfSize:16];
    [_scrollView addSubview:knowLabel];
    [knowLabel release];
    
    //适应人群种类
    UILabel * peopleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, knowLabel.frame.origin.y + knowLabel.frame.size.height + 30, 100, 40)];
    peopleLabel.text = @"适应人群";
    peopleLabel.textColor = [UIColor grayColor];
    peopleLabel.font = [UIFont systemFontOfSize:18];
    [_scrollView addSubview:peopleLabel];
    
    
    NSArray *array = [_secondDic objectForKey:@"crowds"];
    rect = CGRectMake(10, peopleLabel.frame.origin.y + peopleLabel.frame.size.height + 20, 90, 30);
    CGFloat rectX = rect.origin.x;
    
    NSInteger num = [array count];
    
    if (num <= 3) {//适应人群种类小于等于3，则执行
        for (int i = 0; i < num; i++) {
            NSDictionary *dic = [array objectAtIndex:i];
            UILabel *label = [[UILabel alloc] initWithFrame:rect];
            label.backgroundColor = [UIColor yellowColor];
            label.text = [NSString stringWithFormat:@"%@ +%@", [dic objectForKey:@"name"], [dic objectForKey:@"count"]];
            label.textAlignment = NSTextAlignmentCenter;
            [_scrollView addSubview:label];
            rect.origin.x += 100;
        }
    }else if (num <= 6) {//适应人群种类小于等于6，则执行
        
        for (int i = 0; i < 3 ; i++) {
            NSDictionary *dic = [array objectAtIndex:i];
            UILabel *label = [[UILabel alloc] initWithFrame:rect];
            label.backgroundColor = [UIColor yellowColor];
            label.text = [NSString stringWithFormat:@"%@ +%@", [dic objectForKey:@"name"], [dic objectForKey:@"count"]];
            label.textAlignment = NSTextAlignmentCenter;
            [_scrollView addSubview:label];
            rect.origin.x += 100;
        }
        rect.origin.x = rectX;
        rect.origin.y += 40;
        num -= 3;
        for (int i = 0; i < num; i++) {
            NSDictionary *dic = [array objectAtIndex:(i + 3)];
            UILabel *label = [[UILabel alloc] initWithFrame:rect];
            label.backgroundColor = [UIColor yellowColor];
            label.text = [NSString stringWithFormat:@"%@ +%@", [dic objectForKey:@"name"], [dic objectForKey:@"count"]];
            label.textAlignment = NSTextAlignmentCenter;
            [_scrollView addSubview:label];
            rect.origin.x += 100;
        }
        
    }else if (num <= 9) {//适应人群种类小于等于9，则执行
        for (int i = 0; i < 3 ; i++) {
            NSDictionary *dic = [array objectAtIndex:i];
            UILabel *label = [[UILabel alloc] initWithFrame:rect];
            label.backgroundColor = [UIColor yellowColor];
            label.text = [NSString stringWithFormat:@"%@ +%@", [dic objectForKey:@"name"], [dic objectForKey:@"count"]];
            label.textAlignment = NSTextAlignmentCenter;
            [_scrollView addSubview:label];
            rect.origin.x += 100;
        }
        rect.origin.x = rectX;
        rect.origin.y += 40;
        for (int i = 0; i < 3; i++) {
            NSDictionary *dic = [array objectAtIndex:(i + 3)];
            UILabel *label = [[UILabel alloc] initWithFrame:rect];
            label.backgroundColor = [UIColor yellowColor];
            label.text = [NSString stringWithFormat:@"%@ +%@", [dic objectForKey:@"name"], [dic objectForKey:@"count"]];
            label.textAlignment = NSTextAlignmentCenter;
            [_scrollView addSubview:label];
            rect.origin.x += 100;
        }
        rect.origin.x = rectX;
        rect.origin.y += 40;
        num -= 6;
        for (int i = 0; i < num; i++) {
            NSDictionary *dic = [array objectAtIndex:(i + 6)];
            UILabel *label = [[UILabel alloc] initWithFrame:rect];
            label.backgroundColor = [UIColor yellowColor];
            label.text = [NSString stringWithFormat:@"%@ +%@", [dic objectForKey:@"name"], [dic objectForKey:@"count"]];
            label.textAlignment = NSTextAlignmentCenter;
            [_scrollView addSubview:label];
            rect.origin.x += 100;
        }

    }
    //屏幕适应内容长度
    [_scrollView setContentSize:CGSizeMake(0, rect.origin.y + rect.size.height + 100)];
    
}

@end
